package com.nys.cqhttp.service.impl;

import java.util.List;

import com.nys.cqhttp.mapper.UserConfigMapper;
import com.nys.cqhttp.model.UserConfig;
import com.nys.cqhttp.model.UserConfigExample;
import com.nys.cqhttp.util.SpringContextUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.nys.cqhttp.message.factory.MessageFactory;
import com.nys.cqhttp.param.MessageParam;
import com.nys.cqhttp.service.IMsgService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.client.RestTemplate;

/**
 * PrivateMsgServiceImpl
 *
 * @author nysheng
 * @version 1.0.0
 * @description 私聊消息处理业务实现
 * @date 2021/12/27 14:28
 */
@Service("privateMsgServiceImpl")
@Slf4j
public class PrivateMsgServiceImpl implements IMsgService {

    @Autowired
    private MessageFactory messageFactory;
    @Autowired
    private UserConfigMapper userConfigMapper;

    @Override
    public void start(MessageParam messageParam) {
        log.info("收到来自【{}】的消息：【{}】", messageParam.getSenderId(), messageParam.getMessage());
        List<String> messageList = messageFactory.getMessage(messageParam);
        messageList.forEach(message -> {
            if(StringUtils.isNotBlank(message)){
//                cqHttpRepository.sendMsg(messageParam.getMessageType(), messageParam.getSenderId(),
//                        null, message);
                UserConfigExample example = new UserConfigExample();
                UserConfigExample.Criteria criteria = example.createCriteria();
                criteria.andQqEqualTo(messageParam.getSelfId());
                List<UserConfig> userConfigs = userConfigMapper.selectByExample(example);
                RestTemplate restTemplate = SpringContextUtil.getBean("restTemplate");

                restTemplate.getForEntity(userConfigs.get(0).getUrl()+"/send_msg?message_type="+messageParam.getMessageType()
                                +"&user_id="+messageParam.getSenderId()+"&message="+message+"&group_id="+messageParam.getGroupId()
                        , String.class);
            }
        });
    }
}
